package com.dayuanit.dy15.atm.service.impl;

import com.dayuanit.dy15.atm.domain.FlowDomain;
import com.dayuanit.dy15.atm.dto.FlowDto;
import com.dayuanit.dy15.atm.entity.AtmFlow;
import com.dayuanit.dy15.atm.mapper.AtmFlowMapper;
import com.dayuanit.dy15.atm.service.AtmFlowService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * (AtmFlow)表服务实现类
 *
 * @author makejava
 * @since 2020-05-23 11:48:32
 */
@Service("atmFlowService")
public class AtmFlowServiceImpl implements AtmFlowService {
    @Resource
    private AtmFlowMapper atmFlowMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AtmFlow queryById(Long id) {
        return this.atmFlowMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<AtmFlow> queryAllByLimit(int offset, int limit) {
        return this.atmFlowMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param atmFlow 实例对象
     * @return 实例对象
     */
    @Override
    public AtmFlow insert(AtmFlow atmFlow) {
        this.atmFlowMapper.insert(atmFlow);
        return atmFlow;
    }

    /**
     * 修改数据
     *
     * @param atmFlow 实例对象
     * @return 实例对象
     */
    @Override
    public AtmFlow update(AtmFlow atmFlow) {
        this.atmFlowMapper.update(atmFlow);
        return this.queryById(atmFlow.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.atmFlowMapper.deleteById(id) > 0;
    }

    /**
     * 查询最近10笔流水
     * @param cardId
     * @return
     */
    @Override
    public List<FlowDto> listTop10(long cardId) {
        List<FlowDomain> flowDomains = atmFlowMapper.selectByPrimaryKey(cardId);
        if (flowDomains.isEmpty()){
            return Collections.EMPTY_LIST;
        }
        ArrayList<FlowDto> flowDtos = new ArrayList<>(flowDomains.size());
        for (FlowDomain flowDomain : flowDomains) {
            FlowDto flowDto = new FlowDto();
            BeanUtils.copyProperties(flowDomain,flowDto);
            flowDtos.add(flowDto);
        }
        return flowDtos;
    }
}
